<!DOCTYPE html>
<html>
<head>
<script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script>
</head>
<body>
<script>
var barrier = 2;
function frameLoaded()
{
    barrier--;
    if (barrier == 0)
        runTest();
}

function runTest()
{
    test(function(t) {
        canvasTest(document.getElementById("frame1"), true);
    }, 'check if re-parenting canvas keeps its context.');

    test(function(t) {
        canvasTest(document.getElementById("frame2"), false);
    }, 'check if detaching canvas loses its context.');
}

var data;
function canvasTest(frame, adoptCanvas)
{
    var parent = document.getElementById("parent");
    var canvas = frame.contentDocument.getElementById('mycanvas');
    var ctx = canvas.getContext('2d');

    var imageData;
    imageData = ctx.getImageData(100, 100, 1, 1);
    data = imageData.data;
    assert_equals(data[0], 255);
    assert_equals(data[1], 0);
    assert_equals(data[2], 0);

    if (adoptCanvas)
        parent.appendChild(canvas);
    parent.removeChild(frame);

    imageData = ctx.getImageData(100, 100, 1, 1);
    data = imageData.data;
    if (adoptCanvas) {
        assert_equals(data[0], 255);
    } else {
        // GC causes active DOM stop and canvas loses its context.
        assert_equals(data[0], 0);
    }
    assert_equals(data[1], 0);
    assert_equals(data[2], 0);
}
</script>
<div id="parent">
    <iframe id="frame1" src="resources/canvas-fill-for-iframe.html" onload="frameLoaded()"></iframe>
    <iframe id="frame2" src="resources/canvas-fill-for-iframe.html" onload="frameLoaded()"></iframe>
</div>
</body>
</html>
